package com.herohead;



import java.io.ByteArrayInputStream;
import java.io.StringWriter;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
//import javax.xml.transform.Result;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

//import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Xml {

	private String xmlsmtp;
	private String xmltoemail;
	private String xmlfromemail;
	private String xmlsubject;
	private String xmlbody;

	public String getBody() {
		return xmlbody;
	}

	public String getFromEmail() {
		return xmlfromemail;
	}

	public String getSmtp() {
		return xmlsmtp;
	}
	public String getSubject() {
		return xmlsubject;
	}
	public String getToEmail() {
		return xmltoemail;
	}
	public void readEmail(String emailXml) {

		try {

			//File fXmlFile = new File("d:\\file.xml");
			DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
			DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
			Document doc = dBuilder.parse(new ByteArrayInputStream(emailXml.getBytes("UTF-8")));
			//Document doc = dBuilder.parse(emailXml);


			//optional, but recommended
			//read this - http://stackoverflow.com/questions/13786607/normalization-in-dom-parsing-with-java-how-does-it-work
			doc.getDocumentElement().normalize();

			//System.out.println("Root element :" + doc.getDocumentElement().getNodeName());

			NodeList nList = doc.getElementsByTagName("email");

			//System.out.println("----------------------------");

			for (int temp = 0; temp < nList.getLength(); temp++) {

				Node nNode = nList.item(temp);

				//System.out.println("\nCurrent Element :" + nNode.getNodeName());

				if (nNode.getNodeType() == Node.ELEMENT_NODE) {

					Element eElement = (Element) nNode;

					//System.out.println("Staff id : " + eElement.getAttribute("id"));

					xmlsmtp = eElement.getElementsByTagName("smtp").item(0).getTextContent();
					xmltoemail =eElement.getElementsByTagName("toemail").item(0).getTextContent();
					xmlfromemail = eElement.getElementsByTagName("fromemail").item(0).getTextContent();
					xmlsubject = eElement.getElementsByTagName("subject").item(0).getTextContent();
					xmlbody = eElement.getElementsByTagName("body").item(0).getTextContent();

					//System.out.println("Servername : " + eElement.getElementsByTagName("smtp").item(0).getTextContent());
					//System.out.println("To Email : " + eElement.getElementsByTagName("toemail").item(0).getTextContent());
					//System.out.println("From Email : " + eElement.getElementsByTagName("fromemail").item(0).getTextContent());
					//System.out.println("Subject : " + eElement.getElementsByTagName("subject").item(0).getTextContent());
					//System.out.println("Body : " + eElement.getElementsByTagName("body").item(0).getTextContent());

				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public String sendEmail(String toemail, String fromemail, String subject, String body, String smtp) throws TransformerException{

		try {

			DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
			DocumentBuilder docBuilder = docFactory.newDocumentBuilder();

			// root elements
			Document doc = docBuilder.newDocument();
			Element rootElement = doc.createElement("email");
			doc.appendChild(rootElement);

			Element e = doc.createElement("smtp");
			e.appendChild(doc.createTextNode(smtp));
			rootElement.appendChild(e);

			Element e_toemail = doc.createElement("toemail");
			e_toemail.appendChild(doc.createTextNode(toemail));
			rootElement.appendChild(e_toemail);

			Element e_fromemail = doc.createElement("fromemail");
			e_fromemail.appendChild(doc.createTextNode(fromemail));
			rootElement.appendChild(e_fromemail);

			Element e_subject = doc.createElement("subject");
			e_subject.appendChild(doc.createTextNode(subject));
			rootElement.appendChild(e_subject);

			Element e_body = doc.createElement("body");
			e_body.appendChild(doc.createTextNode(body));
			rootElement.appendChild(e_body);




			TransformerFactory transformerFactory = TransformerFactory.newInstance();
			Transformer transformer = transformerFactory.newTransformer();
			//DOMSource source = new DOMSource(doc);

			//StreamResult result = new StreamResult(new String("soutput"));

			DOMSource source = new DOMSource(doc);
			StreamResult result = new StreamResult(new StringWriter());
			transformer.transform(source, result);
			//System.out.println(result.getWriter().toString());




			return result.getWriter().toString();


		} catch (ParserConfigurationException pce) {
			pce.printStackTrace();
			return pce.toString();
		} catch (TransformerConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return e.toString();
		}

	}

	public String statusUpdate(String server, String status) throws TransformerException {
		try {

			DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
			DocumentBuilder docBuilder = docFactory.newDocumentBuilder();

			// root elements
			Document doc = docBuilder.newDocument();
			Element rootElement = doc.createElement("status");
			doc.appendChild(rootElement);

			Element e = doc.createElement("server");
			e.appendChild(doc.createTextNode(server));
			rootElement.appendChild(e);

			Element e_toemail = doc.createElement("status");
			e_toemail.appendChild(doc.createTextNode(status));
			rootElement.appendChild(e_toemail);


			TransformerFactory transformerFactory = TransformerFactory.newInstance();
			Transformer transformer = transformerFactory.newTransformer();
			//DOMSource source = new DOMSource(doc);

			//StreamResult result = new StreamResult(new String("soutput"));

			DOMSource source = new DOMSource(doc);
			StreamResult result = new StreamResult(new StringWriter());
			transformer.transform(source, result);
			//System.out.println(result.getWriter().toString());




			return result.getWriter().toString();
		} catch (ParserConfigurationException pce) {
			pce.printStackTrace();
			return pce.toString();
		} catch (TransformerConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return e.toString();
		}

	}


}

